package richbar.com.richbar.adapter;

import android.app.Activity;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.luck.picture.lib.photoview.PhotoViewAttacher;
import com.okhttp.GlideUtil;

import java.util.ArrayList;
import java.util.List;

import richbar.com.richbar.R;
import richbar.com.richbar.help.Constants;
import richbar.com.richbar.util.NetWorkUtil;


/**
 * Created by QiYang on 17/9/12.
 */
public class PreviewPicAdapter extends PagerAdapter{
    private List<String> path;
    private String id;
    private List<View> views;
    private Activity mActivity;
    private LayoutInflater inflater;
    private PhotoViewAttacher attacher;

    public PreviewPicAdapter(List<String> path, Activity mActivity) {
        this.path = path;
        this.mActivity = mActivity;

        inflater = LayoutInflater.from(mActivity);
        creatViewList();
    }

    private void creatViewList(){
        views = new ArrayList<>();
        for (int i = 0; i < Constants.PHOTO_MAX; i++) {
            View view = inflater.inflate(R.layout.item_photo_view,null);
            views.add(view);
        }
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        View view =views.get(position);
        ImageView iv = view.findViewById(R.id.photoView);
        GlideUtil.loadImageNo(mActivity,path.get(position),iv);
        container.addView(views.get(position));
        iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mActivity.finish();
            }
        });
        return view;
    }

    @Override
    public int getCount() {
        return path == null ? 0 : path.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        // 删除
        container.removeView(views.get(position));
    }
}
